﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BaseText;
using Srvtools;

public partial class WorkTimeN_WorkTimeOvertime : System.Web.UI.Page
{
    #region 系统

    private Srvtools.WebDataSet WMaster;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Request["id"] != null)
            {
                InitializeComponent(true);

                FLWebNavigator1.Visible = true;
                Master.DataSource = WMaster;
                WMaster.SetWhere("OvertimeId=" + Request["id"].ToString());

                string FlowFlag = Request["FlowFlag"].ToString();
                 string EmployeeId = Request["EmpId"].ToString();
                 if (FlowFlag == "" && Convert.ToInt32("0" + EmployeeId) == Convert.ToInt32(Srvtools.CliUtils.fLoginUser))
                 {
                 }
                 else
                 {
                     object o = FLWebNavigator1.NavControls["Comment"];
                    // FLWebNavigator1.NavControls.Clear();
                     FLWebNavigator1.NavControls.Add(o);
                 }


                DataSet ds = WMaster.Execute("SELECT SYS_TODOLIST.LISTID FROM SYS_TODOLIST WHERE  SYS_TODOLIST.FORM_TABLE='WorkTime_Overtime'" +
                    " and SYS_TODOLIST.FORM_KEYS='OvertimeId' and  SYS_TODOLIST.FORM_PRESENTATION='OvertimeId=" + Request["id"].ToString() + "'");
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                {
                    ImageButton imb = new ImageButton();
                    imb.ImageUrl = "../image/uipics/Comment_C.gif";
                    imb.ID = "i";
                    imb.Attributes.Add("onclick", "window.open('../InnerPages/FlowComment.aspx?LISTID=" + ds.Tables[0].Rows[0][0].ToString()
                        + "&ATTACHMENTS=', '', 'resizable=yes,scrollbars=yes,width=645,height=400,top=100,left=200');return false");
                    FLWebNavigator1.Controls.Add(imb);
                }
            }
            else
            {
                InitializeComponent(false);
                Master.DataSource = WMaster;
            }


            MasterPage_MasterPage mpg = (MasterPage_MasterPage)this.Page.Master;
            mpg.HelpHref = "~/Help/WorkTime/加班申请流程.htm";
            mpg.PageName = "加班申请";
            mpg.PicUrl = "";
            mpg.IntiBar();
        }
    }

    private void InitializeComponent(bool action)
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(WorkTimeN_WorkTimeOvertime));
        this.WMaster = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).BeginInit();
        // 
        // WMaster
        // 
        this.WMaster.Active = action;
        this.WMaster.AlwaysClose = true;
        this.WMaster.DeleteIncomplete = true;
        this.WMaster.Guid = null;
        this.WMaster.LastKeyValues = null;
        this.WMaster.Locale = new System.Globalization.CultureInfo("zh-CN");
        this.WMaster.PacketRecords = 100;
        this.WMaster.Position = -1;
        this.WMaster.RemoteName = "SWorkTime.WorkTime_Overtime";
        this.WMaster.ServerModify = true;
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).EndInit();

    }

    #endregion

    #region 事件

    protected void WebFormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        // ZLTextBox startDate = (ZLTextBox)this.WebFormView1.FindControl("ZLTextBox1");
        // ZLTextBox endDate = (ZLTextBox)this.WebFormView1.FindControl("ZLTextBox2");

        DropDownList Sh = (DropDownList)this.WebFormView1.FindControl("StartHour");
        DropDownList SM = (DropDownList)this.WebFormView1.FindControl("StartMinute");
        DropDownList Eh = (DropDownList)this.WebFormView1.FindControl("EndHour");
        DropDownList Em = (DropDownList)this.WebFormView1.FindControl("EndMinute");

        // if (startDate == null || endDate == null)
        //     return;       
        //
        Single hours = this.GetHours(Sh.SelectedValue, SM.SelectedValue, Eh.SelectedValue, Em.SelectedValue);
        e.Values["OvHours"] = hours;
    }


    #endregion

    #region 验证

    private DateTime start = DateTime.MinValue;
    private DateTime end = DateTime.MinValue;

    protected bool ValidateDateFrom(object value)
    {
        //edit insert 必须名称一致 
        WebDropDownList ddsh = (WebDropDownList)this.WebFormView1.FindControl("StartHour");
        WebDropDownList ddsm = (WebDropDownList)this.WebFormView1.FindControl("StartMinute");
        WebDropDownList ddeh = (WebDropDownList)this.WebFormView1.FindControl("EndHour");
        WebDropDownList ddem = (WebDropDownList)this.WebFormView1.FindControl("EndMinute");

        if ((Convert.ToInt32(ddeh.SelectedValue) > Convert.ToInt32(ddsh.SelectedValue)) ||
            (Convert.ToInt32(ddeh.SelectedValue) == Convert.ToInt32(ddsh.SelectedValue) && (Convert.ToInt32(ddem.SelectedValue) == Convert.ToInt32(ddsm.SelectedValue))))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    protected bool ValidateDateTo(object value)
    {
        return true;
        end = DateTime.Parse(value.ToString());
        return this.ValidateDate(start, end);
    }

    private bool ValidateDate(DateTime start, DateTime end)
    {
        if (start != DateTime.MinValue && end != DateTime.MinValue)
        {
            int sHour = start.Hour;
            int sMinute = start.Minute;
            int eHour = end.Hour;
            int eMinute = end.Minute;

            if (sHour < 8 || sHour == 8 && sMinute < 30 || sHour > 17 || eHour < 8 || eHour == 8 && eMinute < 30 || eHour > 17)
            {
                return false;
            }

            if (start.CompareTo(end) >= 0)
            {
                return false;
            }
        }
        return true;
    }

    #endregion

    #region 自定义方法

    private Single GetHours(string StartHour, string StartMinute, string EndHour, string EndMinute)
    {
        Single OutHour = 0;
        int Dsh = int.Parse(StartHour);
        int DsM = int.Parse(StartMinute);
        int DEh = int.Parse(EndHour);
        int DEM = int.Parse(EndMinute);
        OutHour = Convert.ToSingle(DEh - Dsh) + Convert.ToSingle(DEM - DsM) / 60f;

        return OutHour;

    }

    #endregion

    #region 默认值绑定


    protected object GetOvertimeDate()
    {
        return new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).ToShortDateString();
    }

    protected object GetEmployeeId()
    {
        String str = Srvtools.CliUtils.GetValue("_UserCode")[1].ToString();
        str = int.Parse(str).ToString();
        return str;
    }

    #endregion


    protected void WebFormView1_DataBound(object sender, EventArgs e)
    {
        if (WebFormView1.CurrentMode == FormViewMode.Insert || WebFormView1.CurrentMode == FormViewMode.Edit)
        {
            if (Request.Params["MS"] != null)
            {
                WebRefVal tbEmployee = (WebRefVal)this.WebFormView1.FindControl("WebRefVal1");
                if (tbEmployee != null)
                    tbEmployee.ReadOnly = false;
            }
            else
            {
                WebRefVal tbEmployee = (WebRefVal)this.WebFormView1.FindControl("WebRefVal1");
                if (tbEmployee != null)
                    tbEmployee.ReadOnly = true;
            }
        }
    }

    protected void WebFormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
        DropDownList Sh = (DropDownList)this.WebFormView1.FindControl("StartHour");
        DropDownList SM = (DropDownList)this.WebFormView1.FindControl("StartMinute");
        DropDownList Eh = (DropDownList)this.WebFormView1.FindControl("EndHour");
        DropDownList Em = (DropDownList)this.WebFormView1.FindControl("EndMinute");

        Single hours = this.GetHours(Sh.SelectedValue, SM.SelectedValue, Eh.SelectedValue, Em.SelectedValue);
        e.NewValues["OvHours"] = hours;

    }
    protected void FLWebNavigator1_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "cmdApply")
        {
            if (IsPostBack == true)
            {
                if (Request["id"] != null)
                {
                    //Master.Reload();
                    Master.SetWhere("OvertimeId=" + Request["id"].ToString());
                }
                else
                {
                   // Master.Reload();
                }
            }

        }
    }
}
